//
//  StatusOriginalView.swift
//  weibo19
//
//  Created by Arvin on 15/11/14.
//  Copyright © 2015年 Arvin. All rights reserved.
//

import UIKit
import SnapKit
import FFLabel

class StatusOriginalView: UIView, FFLabelDelegate {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        let vc = HomeWebViewController()
        vc.urlStr = text
        label.NavigitionController()?.pushViewController(vc, animated: true)
    }
    
    // 原创微博的底部约束相对对象,根据是否有配图, 动态修改底部约束
    private var constraint: Constraint?
    
    var status: Status? {
        didSet {
            iconView.sd_setImageWithURL(status?.user?.profile_image_URL)
            nameLabel.text = status?.user?.name
            memberView.image = status?.user?.mbrank_image
            timeLabel.text = NSDate.createDate((status?.created_at)!)?.fullDscription()
            if let soucreStr = status?.source?.linkRef() {
                let soucre = "来自" + (soucreStr.link ?? "") + "  " + (soucreStr.clineName ?? "")
                soucreLabel.text = soucre
            }
            verifiedIconView.image = status?.user?.verified_type_image
            contentLabel.attributedText = EmoticonManeger.shareEmoticonManager.stringToImageTextAttrubed(status?.text, font: contentLabel.font)
            // 解除约束
            constraint?.uninstall()
            if status?.pic_URLs?.count == 0 {
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            } else {
                // 传递数据
                pictureView.pic_URLs = status?.pic_URLs
                pictureView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        addSubview(separateView)
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberView)
        addSubview(timeLabel)
        addSubview(soucreLabel)
        addSubview(verifiedIconView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.labelDelegate = self
        
        separateView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.height.equalTo(StatusCellMargin)
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(separateView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
            make.width.equalTo(StatusCellIconViewWidth)
            make.height.equalTo(StatusCellIconViewWidth)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
        }
        memberView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(nameLabel.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
        }

        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconView.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(iconView.snp_bottom)
        }
        
        soucreLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        // 注意: 不要用坐标去约控件的宽高
        verifiedIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }

        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconView.snp_left)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(snp_left).offset(StatusCellMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            constraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }

    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level1"))
    private lazy var nameLabel: UILabel = UILabel(title: "我我我我", size: UIFont.systemFontOfSize(15))
    private lazy var timeLabel: UILabel = UILabel(title: "11:11", size: UIFont.systemFontOfSize(10))
    private lazy var soucreLabel: UILabel = UILabel(title: "来自weibo.com",size: UIFont.systemFontOfSize(10), margin: CGFloat(StatusCellMargin))
    private lazy var verifiedIconView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var contentLabel: FFLabel = FFLabel(title: "哦, 小马哥,小马哥小马哥小马哥小马哥小马哥小马哥小马哥小马哥小马size: 哥", size: UIFont.systemFontOfSize(14), margin: CGFloat(StatusCellMargin))
    private lazy var pictureView: StatusPictureView = StatusPictureView()
    private lazy var separateView: UIView = {
        let s = UIView()
        s.backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        return s
        }()
}
